package cn.lkx.statistics.service.impl;

import cn.lkx.statistics.client.UcenterClient;
import cn.lkx.statistics.entity.StatisticsDaily;
import cn.lkx.statistics.mapper.StatisticsDailyMapper;
import cn.lkx.statistics.service.StatisticsDailyService;
import com.alibaba.nacos.client.naming.utils.RandomUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author lkx
 * @since 2022-05-14
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    UcenterClient ucenterClient;

    @Override
    public void createStatisticsByDay(String day) {
        //先删除当天数据，防止同一天有多跳数据
        baseMapper.delete(new QueryWrapper<StatisticsDaily>().eq("date_calculated",day));
        //获取统计信息
        Random random = new Random();
        Integer registerNum = (Integer) ucenterClient.countRegisterByDay(day).getData().get("countRegister");
        Integer loginNum = random.nextInt(100);
        Integer videoViewNum = random.nextInt(300);//TODO
        Integer courseNum = random.nextInt(100);//TODO
        //创建统计对象
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(registerNum);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);
        //存入库
        baseMapper.insert(daily);

    }
    //根据类型 日期范围的 选择获取统计数据
    @Override
    public Map<String, Object> getChartData(String begin, String end, String type) {
        //查询
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("date_calculated",begin,end);
        queryWrapper.select(type,"date_calculated");
        queryWrapper.orderByAsc("date_calculated");
        List<StatisticsDaily> dailyList = baseMapper.selectList(queryWrapper);
        //封装为map
        List<String> dateList = new ArrayList<>();
        List<Integer> numbList = new ArrayList<>();
        for (int i = 0; i < dailyList.size(); i++) { //遍历多天的每一天
            StatisticsDaily daily = dailyList.get(i);
            dateList.add(daily.getDateCalculated());
            switch (type) {  //根据查询条件 封装
                case "register_num": numbList.add(daily.getRegisterNum()); break;
                case "login_num": numbList.add(daily.getLoginNum()); break;
                case "video_view_num": numbList.add(daily.getVideoViewNum()); break;
                case "course_num": numbList.add(daily.getCourseNum()); break;
                default:break;
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("dateList",dateList);
        map.put("numbList",numbList);

        return map;
    }

}
